import java.util.Random;
import java.util.HashSet;
import java.util.Set;

/**
 * 加减法口算练习系统 v0.3
 * 新增功能：
 * 1. 避免重复算式
 * 2. 控制加减法比例
 */
public class CalculationSystemV03 {

    private static Random random = new Random();
    private static Set<String> equationSet = new HashSet<>();

    /**
     * 模块1：生成随机数
     */
    public static int generateRandomNumber(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 模块2：生成唯一标识符用于重复检测
     */
    public static String generateEquationKey(int num1, int num2, boolean isAddition) {
        return isAddition ? num1 + "+" + num2 : num1 + "-" + num2;
    }

    /**
     * 模块3：生成不重复的算式
     * @param additionRatio 加法题目比例 (0.0-1.0)
     */
    public static String[] generateUniqueEquationWithAnswer(double additionRatio) {
        int maxAttempts = 100; // 防止无限循环
        int attempts = 0;

        while (attempts < maxAttempts) {
            boolean isAddition = random.nextDouble() < additionRatio;
            int num1, num2, answer;
            String equation;

            if (isAddition) {
                num1 = generateRandomNumber(1, 99);
                num2 = generateRandomNumber(1, 100 - num1);
                answer = num1 + num2;
                equation = num1 + " + " + num2 + " = ";
            } else {
                num1 = generateRandomNumber(1, 100);
                num2 = generateRandomNumber(1, num1);
                answer = num1 - num2;
                equation = num1 + " - " + num2 + " = ";
            }

            String equationKey = generateEquationKey(num1, num2, isAddition);

            // 检查是否重复
            if (!equationSet.contains(equationKey)) {
                equationSet.add(equationKey);
                return new String[]{equation, String.valueOf(answer)};
            }

            attempts++;
        }

        // 如果无法生成不重复的算式，返回null
        return null;
    }

    /**
     * 模块4：生成指定加减法比例的习题集
     */
    public static String[][] generateExerciseWithRatio(int count, int problemsPerLine, double additionRatio) {
        String[][] exercises = new String[count][2];
        equationSet.clear(); // 清空之前的记录

        int generated = 0;
        int attempts = 0;
        int maxTotalAttempts = count * 10; // 防止无限循环

        while (generated < count && attempts < maxTotalAttempts) {
            String[] equation = generateUniqueEquationWithAnswer(additionRatio);

            if (equation != null) {
                exercises[generated] = equation;
                generated++;
            }

            attempts++;
        }

        // 如果无法生成足够的不重复题目，用重复题目补全
        if (generated < count) {
            System.out.println("警告：无法生成足够的不重复题目，已生成 " + generated + " 道不重复题目");
            // 这里可以添加补全逻辑
        }

        return exercises;
    }

    /**
     * 模块5：格式化输出
     */
    public static void printFormattedEquations(String[][] exercises, int problemsPerLine) {
        int totalProblems = exercises.length;

        System.out.println("加减法口算练习系统-v0.3");
        System.out.println("题目：");
        System.out.println("======================");

        // 统计加减法数量
        int additionCount = 0;
        int subtractionCount = 0;

        for (String[] exercise : exercises) {
            if (exercise[0].contains("+")) {
                additionCount++;
            } else {
                subtractionCount++;
            }
        }

        // 输出题目
        for (int i = 0; i < totalProblems; i++) {
            if (exercises[i] != null) {
                System.out.printf("%2d. %-15s", i + 1, exercises[i][0]);

                if ((i + 1) % problemsPerLine == 0) {
                    System.out.println();
                }
            }
        }
        System.out.println("\n");

        // 输出答案
        System.out.println("答案：");
        System.out.println("======================");
        for (int i = 0; i < totalProblems; i++) {
            if (exercises[i] != null) {
                System.out.printf("%2d. %-15s", i + 1, exercises[i][0] + exercises[i][1]);

                if ((i + 1) % problemsPerLine == 0) {
                    System.out.println();
                }
            }
        }

        System.out.println("\n======================");
        System.out.println("统计信息：");
        System.out.println("加法题目: " + additionCount + " 道");
        System.out.println("减法题目: " + subtractionCount + " 道");
        System.out.println("总计: " + totalProblems + " 道题目");
    }

    public static void main(String[] args) {
        // 生成30道题目，加法比例70%，每行3个
        String[][] exercises = generateExerciseWithRatio(30, 3, 0.7);
        printFormattedEquations(exercises, 3);
    }
}
